<?php

/**
 * Description of equipaModel
 *
 * @author Leonardo Lino
 */
require_once 'gestorDatabase.php';

class equipaModel extends gestorDatabase {

    public function loadAll() {
        $query = $this->db->query('SELECT *
            FROM equipas_tbl ORDER BY data_inscricao');

        if ($query->result_array() != null) {
            return $query->result_array();
        }
        return null;
    }

    public function loadAllInactive() {
        $query = $this->db->query('SELECT *
            FROM equipas_tbl where ativo=0');

        if ($query->result_array() != null) {
            return $query->result_array();
        }
        return null;
    }

    public function lastRegistados($qty) {
        $sql = 'SELECT * FROM equipas_tbl ORDER BY data_inscricao limit 0 , ?';
        $query = $this->db->query($sql, array($qty));
        return $query->result_array();
    }

    public function loadFromId($id) {
        $sql = 'SELECT * FROM equipas_tbl where id=?';
        $query = $this->db->query($sql, array($id));
        $row = $query->row();
        if ($row != null) {
            return $row;
        }
        return null;
    }

    public function deleteFromId($id) {
        $sql = 'DELETE FROM equipas_tbl WHERE id = ?';
        $query = $this->db->query($sql, array($id));
        return true;
    }

    public function getEquipaFromUid($id) {
        $sql = 'SELECT * FROM equipas_tbl where uid=?';
        $query = $this->db->query($sql, array($id));
        $row = $query->row();
        if ($row != null) {
            return $row;
        }
        return null;
    }

    public function ativarEquipa($id) {
        $sql = "UPDATE `equipas_tbl` SET ativo=1  WHERE id=?";
        $this->db->query($sql, array($id));
        return true;
    }

    public function novo($equipa, $password, $email, $escola, $morada, $localidade, $codpostal, $professor, $telefone, $elementos, $avatar_url) {
        $today = date("Y-m-d");

        $this->db->trans_start();
        $this->user->createUser($equipa, $password, 1);
        $last_id = $this->db->insert_id();

        $sql = "INSERT INTO `equipas_tbl` (uid, equipa, email, escola, morada, localidade, codpostal, professor, telefone, elementos, ativo, avatar_url, data_inscricao) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";

        $this->db->query($sql, array($last_id, $equipa, $email, $escola, $morada, $localidade, $codpostal, $professor, $telefone, $elementos, 0, $avatar_url, $today));
        $this->db->trans_complete();

        return true;
    }

    public function update($equipa, $email, $escola, $morada, $localidade, $codpostal, $professor, $telefone, $elementos, $id) {
        $today = date("Y-m-d");
        $sql = "UPDATE `equipas_tbl` SET equipa=?, email=?, escola=?,
          morada=?, localidade=?, codpostal=?, professor=?, telefone=?,   
          elementos=?, data_mod=? WHERE id=?";

        return $this->db->query($sql, array($equipa, $email, $escola, $morada, $localidade,
                    $codpostal, $professor, $telefone, $elementos, $today, $id));
    }

}

?>
